using Unity.Cinemachine;
using UnityEngine;

public class CameraManager : ManagerBase
{
    public GameObject DeadVirtualCameraPrefab;
    public GameObject DialogueCameraPrefab;
    public GameObject EditorCameraPrefab;
    public CinemachineCamera MainVirtualCamera{ get; set; }
    public CinemachineCamera DeadVirtualCamera { get; set; }
    public CinemachineCamera DialogueCamera { get; set;}
    public CinemachineCamera EditorCamera { get; set;}
    public GameObject cameraPrefab { get; private set; }
    private GameObject CameraRoot;
    private Camera cam;
    private Transform Home;
    public override void OnAwake()
    {
        base.OnAwake();
        cam = Camera.main;
        if (!cam.TryGetComponent<CinemachineBrain>(out var brain))
        {
            cam.gameObject.AddComponent<CinemachineBrain>();
        }

        if (CameraRoot == null)
        {
            CameraRoot = new GameObject("CameraRoot");
            CameraRoot.transform.parent = GameRoot.Instance.transform;
        }
        if (MainVirtualCamera == null)
        {
            cameraPrefab = gameRoot.stageSystem.currentStageConfig.camera;
            var go = GameObject.Instantiate(cameraPrefab, CameraRoot.transform);
            MainVirtualCamera = go.GetComponent<CinemachineCamera>();
            SetLookAt();
        }
        else
        {
            SetLookAt();
        }
      
        InitDeadCamera();
        InitEditorCamera();
        InitDialogueCamera();
    }
    public override void OnShow()
    {
        base.OnShow();
        if (gameRoot.currentGameMode?.currentGameMode == GameMode.GamePlay)
        {
            Home = GameObject.FindWithTag("Home").transform;
        }
        EnableMainCamera();
    }

  

    public CinemachineCamera GetVirtualCamera()
    {
        return MainVirtualCamera;
    }
    public override void UnInit()
    {
        if (CameraRoot != null)
        {
            GameObject.Destroy(CameraRoot);
        }
      
    }
    private void SetLookAt()
    {
        if (roleSystem.GetDefaultRoleConfig().lookAt != null)
        {
            MainVirtualCamera.LookAt = roleSystem.GetDefaultRoleConfig().lookAt;
        }
        if (roleSystem.GetDefaultRoleConfig().followAt != null)
        {
            MainVirtualCamera.Follow = roleSystem.GetDefaultRoleConfig().followAt;
        }
    }

    public void InitDeadCamera()
    {
        if (DeadVirtualCameraPrefab == null)
        {
            return;
        }
        GameObject go = GameObject.Instantiate(DeadVirtualCameraPrefab, CameraRoot.transform);
        DeadVirtualCamera = go.GetComponent<CinemachineCamera>();

        DeadVirtualCamera.LookAt = Home;
        DeadVirtualCamera.Follow = Home;
    }
    public void InitEditorCamera()
    {
        if (EditorCameraPrefab == null)
        {
            return;
        }
        GameObject go = GameObject.Instantiate(EditorCameraPrefab, CameraRoot.transform);
        EditorCamera = go.GetComponent<CinemachineCamera>();
        EditorCamera.gameObject.SetActive(false);
    }
    private void InitDialogueCamera()
    {
        if (DialogueCameraPrefab == null)
        {
            return;
        }
        GameObject go = GameObject.Instantiate(DialogueCameraPrefab, CameraRoot.transform);
        DialogueCamera = go.GetComponent<CinemachineCamera>();
    }
    public void EnableEditorCamera()
    {
        EditorCamera.gameObject.SetActive(true);
        var player = roleSystem.GetDefaultRoleConfig().roleGO;
        EditorCamera.transform.position = new Vector3(player.transform.position.x, player.transform.position.y +0.5f, -1);
        EditorCamera.Priority = 10;
        DeadVirtualCamera.Priority = 0;
        MainVirtualCamera.Priority = 0;
        DialogueCamera.Priority = 0;
      
    }
    public void EnableDeadCamera()
    {
        DeadVirtualCamera.Priority = 10;
        MainVirtualCamera.Priority = 0;
        DialogueCamera.Priority = 0;
        EditorCamera.Priority = 0;
        EditorCamera.gameObject.SetActive(false);
    }
    public void EnableMainCamera()
    {
        MainVirtualCamera.Priority = 10;
        if (DeadVirtualCamera != null)
        {
            DeadVirtualCamera.Priority = 0;
        }
        if (EditorCamera != null)
        {
            EditorCamera.Priority = 0;
            EditorCamera.gameObject.SetActive(false);
        }
        DialogueCamera.Priority = 0;
     
    }
    public void EnableDialogueCamera(Transform interactive)
    {
        DialogueCamera.LookAt = interactive;
        DialogueCamera.Follow = interactive;
        DialogueCamera.Priority = 10;
        if (DeadVirtualCamera != null)
        {
            DeadVirtualCamera.Priority = 0;
        }
        if (EditorCamera != null)
        {
            EditorCamera.Priority = 0;
            EditorCamera.gameObject.SetActive(false);
        }
        MainVirtualCamera.Priority = 0;
    }
   
}
